package com.example.g2daylx2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.g2daylx2.databinding.ActivityMainBinding
import com.example.lib_base.net.mvvm.BaseViewActivity
import dagger.hilt.android.AndroidEntryPoint
import java.util.Timer
import kotlin.concurrent.timerTask

@Route(path = "/app/MainActivity")
@AndroidEntryPoint
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    var timer : Timer?=null
    var time =5
    override fun initView() {
        if (SPUtils.getInstance().getBoolean("isIn",false)){
            ARouter.getInstance().build("/app/MainActivity2").navigation()
            finish()
        }
        timer = Timer()
        timer!!.schedule(timerTask {
            runOnUiThread {
                binding.tvTime.setText("${time}s")
                if (time==0){
                    timer!!.cancel()
                    ARouter.getInstance().build("/app/MainActivity2").navigation()
                    finish()
                }
                time--
            }

        },0,1000)
        binding.tvTime.setOnClickListener {
            start()
        }
    }
    fun start(){
        SPUtils.getInstance().put("isIn",true)
        timer!!.cancel()
        ARouter.getInstance().build("/app/MainActivity2").navigation()
        finish()
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (timer!=null){
            timer!!.cancel()
            timer=null
        }
    }
}